package com.jack.bookshop.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

// 省略了import
@WebServlet("/captcha.do")
public class CaptchaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 220; // 生成的图片的宽度
    public static final int HEIGHT = 80; // 生成的图片的高度
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 在内存中创建一张图片
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        // 得到图片
        Graphics g = image.getGraphics();
        // 1.设置图片的背景色
        setBackGround(g);
        // 2.设置图片的边框
        setBorder(g);
        // 3.在图片上画干扰线
        drawRandomLine(g);
        // 4.在图片上写随机数
        String random = drawRandomNum((Graphics2D) g);
        // 5.将随机数存在session中
        request.getSession().setAttribute("validate", random);
        // 5.图形写给浏览器
        response.setContentType("image/jpeg");
        // 6.发响应头控制浏览器不要缓存图片
        response.setDateHeader("expries", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        ImageIO.write(image, "jpg", response.getOutputStream());
    }

    /*
     * 设置图片的背景色
     */
    private void setBackGround(Graphics g) {
        // 设置颜色
        g.setColor(Color.WHITE);
        // 填充区域
        g.fillRect(0, 0, WIDTH, HEIGHT);
    }

    /*
     * 设置图片的边框
     */
    private void setBorder(Graphics g) {
        // 设置边框颜色
        g.setColor(Color.BLUE);
        // 边框区域
        g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
    }

    /*
     * 在图片上画干扰线
     */
    private void drawRandomLine(Graphics g) {
        // 设置颜色
        g.setColor(Color.red);

        // 设置线条个数并画线
        for(int i=0;i<30;i++) {
            // 生成干扰线随机的起始坐标
            int x1 = new Random().nextInt(WIDTH); // 生成0~WIDTH(不包括WIDTH)的随机数
            int y1 = new Random().nextInt(HEIGHT);

            // 生成干扰线随机的结束坐标
            int x2 = new Random().nextInt(WIDTH);
            int y2 = new Random().nextInt(HEIGHT);

            g.drawLine(x1, y1, x2, y2);
        }
    }

    /*
     * 在图片上写随机数
     */
    private String drawRandomNum(Graphics2D g) {
        // 设置颜色
        g.setColor(Color.RED);
        // 设置字体
        g.setFont(new Font("Courier New", Font.BOLD, 40));
        // 验证码全是大写字母
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuffer sb = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        // 控制字数
        for(int i=0;i<4;i++) {
            char ch = base.charAt(new Random().nextInt(base.length()));
            sb.append(ch).append(" ");
            sb2.append(ch);
        }
        g.drawString(sb.toString(),20,60);
        return sb2.toString();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
